package com.example.home.mvvm.weights;

import android.content.Context;
import android.graphics.drawable.StateListDrawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.home.R;


public class TabItemsView extends RelativeLayout {
    private static final String TAG = "TabItemsView";
    private int mIndex;
    private View mView;

    public TabItemsView(Context context) {
        this(context, null);
    }

    public TabItemsView(Context context, int index) {
        this(context, null);
        mIndex = index;
        mView = LayoutInflater.from(context).inflate(R.layout.home_tab_items, this, true);
        initView();
    }

    public TabItemsView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    private void initView() {
        ImageView tv_icon = (ImageView) mView.findViewById(R.id.iv_tab_icon);
        TextView tv_tittle = (TextView) mView.findViewById(R.id.tv_tab_tittle);
        StateListDrawable drawableHome = new StateListDrawable();
        StateListDrawable drawableAlarm = new StateListDrawable();
        StateListDrawable drawableMsg = new StateListDrawable();
        StateListDrawable drawableKng = new StateListDrawable();
        StateListDrawable drawableMy = new StateListDrawable();
        drawableHome.addState(new int[]{android.R.attr.state_selected}, getResources().getDrawable(R.mipmap.home_s));
        drawableHome.addState(new int[]{android.R.attr.state_focused}, getResources().getDrawable(R.mipmap.home_s));
        drawableHome.addState(new int[]{-android.R.attr.state_selected}, getResources().getDrawable(R.mipmap.home_n));
        drawableHome.addState(new int[]{-android.R.attr.state_focused}, getResources().getDrawable(R.mipmap.home_n));

        drawableAlarm.addState(new int[]{android.R.attr.state_selected}, getResources().getDrawable(R.mipmap.alarm_s));
        drawableAlarm.addState(new int[]{android.R.attr.state_focused}, getResources().getDrawable(R.mipmap.alarm_s));
        drawableAlarm.addState(new int[]{-android.R.attr.state_selected}, getResources().getDrawable(R.mipmap.alarm_n));
        drawableAlarm.addState(new int[]{-android.R.attr.state_focused}, getResources().getDrawable(R.mipmap.alarm_n));

        drawableMsg.addState(new int[]{android.R.attr.state_selected}, getResources().getDrawable(R.mipmap.message_s));
        drawableMsg.addState(new int[]{android.R.attr.state_focused}, getResources().getDrawable(R.mipmap.message_s));
        drawableMsg.addState(new int[]{-android.R.attr.state_selected}, getResources().getDrawable(R.mipmap.message_n));
        drawableMsg.addState(new int[]{-android.R.attr.state_focused}, getResources().getDrawable(R.mipmap.message_n));

        drawableKng.addState(new int[]{android.R.attr.state_selected}, getResources().getDrawable(R.mipmap.knowledge_s));
        drawableKng.addState(new int[]{android.R.attr.state_focused}, getResources().getDrawable(R.mipmap.knowledge_s));
        drawableKng.addState(new int[]{-android.R.attr.state_selected}, getResources().getDrawable(R.mipmap.knowledge_n));
        drawableKng.addState(new int[]{-android.R.attr.state_focused}, getResources().getDrawable(R.mipmap.knowledge_n));

        drawableMy.addState(new int[]{android.R.attr.state_selected}, getResources().getDrawable(R.mipmap.my_s));
        drawableMy.addState(new int[]{android.R.attr.state_focused}, getResources().getDrawable(R.mipmap.my_s));
        drawableMy.addState(new int[]{-android.R.attr.state_selected}, getResources().getDrawable(R.mipmap.my_n));
        drawableMy.addState(new int[]{-android.R.attr.state_focused}, getResources().getDrawable(R.mipmap.my_n));
        switch (mIndex) {
            case 0:
                tv_icon.setImageDrawable(drawableHome);
                break;
            case 1:
                tv_icon.setImageDrawable(drawableAlarm);
                break;
            case 2:
                tv_icon.setImageDrawable(drawableMsg);
                break;
            case 3:
                tv_icon.setImageDrawable(drawableKng);
                break;
            case 4:
                tv_icon.setImageDrawable(drawableMy);
                break;
        }
        tv_tittle.setText(getResources().getStringArray(R.array.home_tab_title)[mIndex]);
    }

}
